const sessionStore = {
  //存储过期时间映射
  setExpireMap: (key, expire) => {
    const expireMap = localStorage.getItem('EXPIRE_MAP') || "{}"
    localStorage.setItem(
      'EXPIRE_MAP',
      JSON.stringify({
        ...JSON.parse(expireMap),
        key: expire
      })
    )
  },
  setItem: (key, value) => {
    let now = new Date();
    let tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
    let expireTime = tomorrow.getTime();
    sessionStore.setExpireMap(key, expireTime)
    localStorage.setItem(key, value)
  },
  getItem: () => {
    // 在取值前判断是否过期
    const expireMap = JSON.parse(
      localStorage.getItem('EXPIRE_MAP')
    ) ? JSON.parse(
      localStorage.getItem('EXPIRE_MAP')
    ) : "{}"
    if (new Date().getTime() > expireMap.key || expireMap == "{}") {
      localStorage.removeItem("EXPIRE_MAP");
      localStorage.removeItem("loginNumber");
      return null
    } else {
      return true
    }
  }
}

export default sessionStore;
